/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import com.google.common.collect.Maps;
import io.renren.common.utils.Result;
import io.renren.core.dto.LearnKnowSourceDTO;
import io.renren.core.dto.LearnKnowTypeDTO;
import io.renren.core.service.LearnKnowSourceService;
import io.renren.core.service.LearnKnowTypeService;
import io.renren.core.service.LearnNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * banner
 */
@RestController
@RequestMapping("/api/know")
@Api(tags = "知识库")
public class ApiKnowController {

    @Autowired
    private LearnKnowSourceService learnKnowSourceService;
    @Autowired
    private LearnKnowTypeService learnKnowTypeService;
    @Autowired
    private LearnNewsService learnNewsService;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public Result<?> list() {
        List<LearnKnowSourceDTO> sourceList = learnKnowSourceService.list(Maps.newHashMap());
        List<LearnKnowTypeDTO> typeList = learnKnowTypeService.list(Maps.newHashMap());
        sourceList.sort(Comparator.comparing(LearnKnowSourceDTO::getKnowDate));
        Collections.reverse(sourceList);

        Map<Long, LearnKnowTypeDTO> typeMap = Maps.newHashMap();
        typeList.forEach(x -> {
            x.setKnowList(Lists.newArrayList());
            typeMap.put(x.getId(), x);
        });
        sourceList.forEach(x -> {
            if (typeMap.containsKey(x.getKnowTypeId())) {
                typeMap.get(x.getKnowTypeId()).getKnowList().add(x);
            }
        });

        typeList.sort(Comparator.comparing(LearnKnowTypeDTO::getSort));
        Collections.reverse(typeList);
        return new Result<>().ok(typeList);
    }

    @GetMapping("know-by-id")
    @ApiOperation(value = "获取知识库数据")
    public Result<?> knowById(Long id) {
        return new Result<>().ok(learnKnowSourceService.get(id));
    }


}